/**
 * FileName: TokenUtils
 * Author:   SAGIRI
 * Date:     2019/5/15 0015 0:31
 * Description: token工具类
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.ruimei.util;

import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * 〈token工具类〉
 *
 * @author SAGIRI
 * @create 2019/5/15 0015
 * @since 1.0.0
 */
public class TokenUtils {

    public  static String getTeacherId(HttpServletRequest request){
        String teacherId = null;
        String token ;
        String header = request.getHeader("token");
        try {
            token =   Base64Encoder.decoder(header);
            if (token.indexOf("teacher") != -1){
                teacherId = token.substring(token.indexOf("_")+1,token.length());
            }

        }catch (IOException e){
            e.printStackTrace();
        }
        if(StringUtils.isNotBlank(teacherId)){
            return teacherId;
        }
        return null;
    }

    public  static String getParentId(HttpServletRequest request){
        String parentId = null;
        String token ;
        String header = request.getHeader("token");
        try {
            token =   Base64Encoder.decoder(header);
            if (token.indexOf("parent") != -1){
                parentId = token.substring(token.indexOf("_")+1,token.length());
            }


        }catch (IOException e){
            e.printStackTrace();
        }
        if(StringUtils.isNotBlank(parentId)){
            return parentId;
        }
        return null;
    }

  public static void main(String[] args) {
    System.out.println( Base64Encoder.encoder("parent_"+"65d4d6436ff04adf8aabc01aec1c678f"));
  }
}